﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using Telerik.Windows;
using System.Diagnostics;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for HRM_EmpSearch.xaml
    /// </summary>
    public partial class HRM_EmpSearch : Window
    {
        public HRM_EmpSearch()
        {
            InitializeComponent();
            btnEditEmp.Visibility = Visibility.Hidden;
            btnDispPhoto.Visibility = Visibility.Hidden;

        }
        public static string Finalpath="";
        private void radGridView1_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            btnEditEmp.Visibility = Visibility.Visible;
            btnDispPhoto.Visibility = Visibility.Visible;
            if (grdEmp.SelectionMode == SelectionMode.Single)
            {
           DataTable mydtPersonal=SearchEmployeeDAL.LoadAllEmpDataFromDB(5, ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString());
            DataTable mydtOrg = SearchEmployeeDAL.LoadAllEmpDataFromDB(6, ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString());
            DataTable mydtComm=SearchEmployeeDAL.LoadAllEmpDataFromDB(7, ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString());
            if (mydtPersonal.Rows.Count > 0)
            {
                lblPFnameRes.Content = mydtPersonal.Rows[0][0].ToString();
                lblPDOBRes.Content = mydtPersonal.Rows[0][1].ToString();
                lblPStatusRes.Content = mydtPersonal.Rows[0][2].ToString();
                String appStartPath = AppDomain.CurrentDomain.BaseDirectory +mydtPersonal.Rows[0][3].ToString();
               Finalpath=appStartPath.Replace("\\bin\\Debug", "");             
                ImageSourceConverter imgs = new ImageSourceConverter();
                ShowImg.SetValue(Image.SourceProperty, imgs.ConvertFromString(Finalpath));
            }
            if (mydtOrg.Rows.Count > 0)
            {
                lblODOJRes.Content = mydtOrg.Rows[0][0].ToString();
                lblODesRes.Content = mydtOrg.Rows[0][1].ToString();
                lblORepToRes.Content = mydtOrg.Rows[0][2].ToString();
            }
            if (mydtComm.Rows.Count > 0)
            {
                lblCAddressRes.Content = mydtComm.Rows[0][0].ToString() + ", " + mydtComm.Rows[0][1].ToString() + ", " + mydtComm.Rows[0][2].ToString() + ", \n" + mydtComm.Rows[0][3].ToString() + ", " + mydtComm.Rows[0][4].ToString() + ", " + mydtComm.Rows[0][5].ToString();
                lblCMbWRes.Content = mydtComm.Rows[0][6].ToString();
                lblCMbHmRes.Content = mydtComm.Rows[0][7].ToString();
                lblCLndHomeRes.Content = mydtComm.Rows[0][8].ToString();
                lblCLndWkRes.Content = mydtComm.Rows[0][9].ToString();
                lblCPEmRes.Content = mydtComm.Rows[0][10].ToString();
                lblCOfEmRes.Content = mydtComm.Rows[0][11].ToString();
            }
            }      
          
               
           
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {  
                DataTable mydt = loadDataintoTable();         
           
          if (cmbEType.Text == "Permanent Employee")
            {
              for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (mydt.Rows[i][0].ToString().Contains("CE"))
                    {
                        mydt.Rows.Remove(mydt.Rows[i]);
                    }
                }               
            }
            else if (cmbEType.Text == "Contract Employee")
            {
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (mydt.Rows[i][0].ToString().Contains("PE"))
                    {
                        mydt.Rows.Remove(mydt.Rows[i]);
                    }
                }               
            }

            grdEmp.ItemsSource = mydt;
           
              
        }

        private DataTable loadDataintoTable()
        {
            DataTable mydt = new DataTable();
            mydt.Clear();

            if (txtEName.Text == "" && txtENo.Text == "")
            {
                mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(1);
            }
            else
                if (txtEName.Text != "" && txtENo.Text == "")
                {
                    mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(txtEName.Text, 2);
                }
                else
                    if (txtENo.Text != "" && txtEName.Text == "")
                    {
                        mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(3, txtENo.Text);
                    }
                    else
                    {
                        mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(4, txtEName.Text, txtENo.Text);
                    }

            return mydt;
        }

        private void btnClrSearch_Click(object sender, RoutedEventArgs e)
        {
            cmbEType.SelectedIndex = 0;
            txtEName.Text = "";
            txtENo.Text = "";
        }

        private void btnEditEmp_Click(object sender, RoutedEventArgs e)
        {
           
        }      

        private void btnDispPhoto_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(Finalpath);
        }
    }
}
